using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using QLNS.Data;
using QLNS.Object;
using System.Windows.Forms;

namespace QLNS.Control
{
    class TamUngCtrl
    {
        TamUngData data = new TamUngData();

        BindingSource bs = new BindingSource();
        public void HienthiTamUngDataGridview(DataGridView dg, BindingNavigator bn)
        {
            BindingSource bs = new BindingSource();
            DataTable tbl = data.DanhSachTamUng();
            bs.DataSource = tbl;
            bn.BindingSource = bs;
            dg.DataSource = bs;

        }
        public void HienthiTamUngDataGridview(System.Windows.Forms.DataGridView dg, System.Windows.Forms.BindingNavigator bn,
            ComboBox cmbMaNhanVien, TextBox txtTamUng, TextBox txtLyDo,DateTimePicker ngay)
        {
            System.Windows.Forms.BindingSource bs = new System.Windows.Forms.BindingSource();
            bs.DataSource = data.DanhSachTamUng();


            cmbMaNhanVien.DataBindings.Clear();
            cmbMaNhanVien.DataBindings.Add("SelectedValue", bs, "ID");

            txtTamUng.DataBindings.Clear();
            txtTamUng.DataBindings.Add("Text", bs, "TamUng");

            ngay.DataBindings.Clear();
            ngay.DataBindings.Add("Value", bs, "NgayTamUng");

            txtLyDo.DataBindings.Clear();
            txtLyDo.DataBindings.Add("Text", bs, "LyDoTamUng");

            bn.BindingSource = bs;
            dg.DataSource = bs;
        }
        public double LayTongTamUng(string MaNhanVien, DateTime Thang)
        {
            DataTable tbl = data.TimMa(MaNhanVien);
            double TamUng = 0;
            foreach (DataRow row in tbl.Rows)
            {
                if (Thang.Month == Convert.ToDateTime(row["NgayTamUng"]).Month && Thang.Year == Convert.ToDateTime(row["NgayTamUng"]).Year)
                    TamUng += Convert.ToDouble(row["TamUng"]);
            }
            return TamUng;
        }

        public IList<TamUngInfo> LayDanhTamUng()
        {
            DataTable tbl = data.DanhSachTamUng();
            IList<TamUngInfo> ds = new List<TamUngInfo>();

            foreach (DataRow row in tbl.Rows)
            {
                TamUngInfo tamung = new TamUngInfo();
                tamung.NgayTamUng = Convert.ToDateTime(row["NgayTamUng"]);
                tamung.ID = Convert.ToString(row["ID"]);
                tamung.TamUng = Convert.ToDouble(row["TamUng"]);
                tamung.LyDoTamUng = Convert.ToString(row["LyDoTamUng"]);
                ds.Add(tamung);
            }
            return ds;
        }
        public string TimMa(String ma)
        {
            data.TimMa(ma);
            if (data != null)
                return ma;
            return null;
        }

        public DateTime TimNgay(DateTime ngay)
        {
            data.TimNgay(ngay);
            if (data != null)
                return ngay;
            return Convert.ToDateTime(01/01/1900);
        }

        public DataRow NewRow()
        {
            return data.NewRow();
        }
        public void Add(DataRow row)
        {
            data.Add(row);
        }
        public bool Save()
        {
            return data.Save();
        }
        public void Update()
        {
            bs.MoveNext();
            data.Save();
        }
    }
}
